package dq.study.minimvc.handler;

import dq.study.minimvc.context.ContextLoader;
import dq.study.minimvc.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class RequestMappingHandlerMapping extends AbstractHandlerMapping {

    public RequestMappingHandlerMapping() {
        super();
    }

    @Override
    protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
        String url = request.getRequestURI();
        if (request.getContextPath()!=null)
            url = "/" + StringUtils.deleteStartTrim(url,request.getContextPath());
        Map<String, Object> handlers = ContextLoader.getInstance().getHandlers();
        if (handlers.containsKey(url)){
            return handlers.get(url);
        }
        return null;
    }
}
